package java.atguigu.algorithm.List;

public class MergeSortedArray {
    public static void main(String[] args) {
        int nums1[]=new int[]{1,2,3,0,0,0};
        int nums2[]=new int[]{2,5,6};
        merge(nums1,3,nums2,3);
        System.out.println(nums1);
    }
    public static void merge(int[] nums1,int m,int[] nums2,int n){
        int[] res=new int[m+n];
        int left=0,right=0;
        int current=0;
        while(left<m&&right<n){
            if(nums1[left]<nums2[right]){
                res[current]=nums1[left];
                left++;
                current++;
            }
            else if(nums1[left]>nums2[right]){
                res[current]=nums2[right];
                right++;
                current++;
            }else{
                res[current]=nums1[left];
                current++;
                left++;
                res[current]=nums2[right];
                current++;
                right++;
            }
        }
            while(left<m){
                res[current]=nums1[left];
                current++;
                left++;
            }
            while(right<n){
                res[current]=nums2[right];
                current++;
                right++;
            }
        for(int i=0;i<m+n;i++){
            nums1[i]=res[i];
        }
    }
}
